package com.backend.demo.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.stereotype.Controller;

import com.backend.demo.entities.EnumItem;
import com.backend.demo.service.IEnumItemService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 枚举项Action
 * @author Administrator
 *
 */
@ParentPackage("user")
@Namespace("/jspView")
@InterceptorRefs(value = { @InterceptorRef(value = "loginStack") })
@Controller
public class EnumItemAction extends ActionSupport implements
		ModelDriven<EnumItem>, RequestAware {

	private static final long serialVersionUID = 1L;
	
	private Map<String,Object> request;
	
	private EnumItem enumItem;
	
	@Resource(name="enumItemService")
	private IEnumItemService enumItemService;
	
	public void setEnumItem(EnumItem enumItem) {
		this.enumItem = enumItem;
	}
	
	public EnumItem getEnumItem() {
		return enumItem;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public EnumItem getModel() {
		if(enumItem == null){
			enumItem = new EnumItem();
		}
		return enumItem;
	}
	
	@Action(value="getAllEnumItems",results={@Result(name=SUCCESS,location="/jspView/dict/enumItem/enumItemList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getAllEnumItems(){
		List<EnumItem> enumItems = enumItemService.getAllEnumItems();
		request.put("enumItems", enumItems);
		return SUCCESS;
	}
	
	@Action(value="getEnumItemsByCondition",results={@Result(name=SUCCESS,location="/jspView/dict/enumItem/enumItemList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getEnumItemsByCondition(){
		List<EnumItem> enumItems = enumItemService.getEnumItemsByCondition(enumItem);
		request.put("enumItems", enumItems);
		return SUCCESS;
	}
	
	@Action(value="getEnumItemByEnumItemID",results={@Result(name=SUCCESS,location="/jspView/dict/enumItem/editEnumItem.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getEnumItemByEnumItemID(){
		EnumItem enumItemNew = enumItemService.getEnumItemByEnumItemID(enumItem.getEnumid());
		request.put("enumItem", enumItemNew);
		return SUCCESS;
	}
	
	@Action(value="addEnumItem",results={@Result(name=SUCCESS,location="/jspView/getAllEnumItems"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String addEnumItem(){
		int num = enumItemService.addEnumItem(enumItem);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="editEnumItem",results={@Result(name=SUCCESS,location="/jspView/getAllEnumItems"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String editEnumItem(){
		int num = enumItemService.editEnumItem(enumItem);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="deleteEnumItemByEnumItemID",results={@Result(name=SUCCESS,location="/jspView/getAllEnumItems"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String deleteEnumItemByEnumItemID(){
		int num = enumItemService.deleteEnumItemByEnumID(enumItem.getEnumid());
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="redirectEnumItem",results={@Result(name=SUCCESS,location="/jspView/dict/enumItem/addEnumItem.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String redirectEnumItem(){
		return SUCCESS;
	}
}
